package com.csh.subject.domain.handler.subject;

import com.csh.subject.common.enums.IsDeletedFlagEnum;
import com.csh.subject.common.enums.SubjectInfoTypeEnum;
import com.csh.subject.domain.convert.SubjectJudgeConvert;
import com.csh.subject.domain.entity.SubjectAnswerBo;
import com.csh.subject.domain.entity.SubjectInfoBo;
import com.csh.subject.domain.entity.SubjectOptionBo;
import com.csh.subject.infra.basic.entity.SubjectJudge;
import com.csh.subject.infra.basic.service.SubjectJudgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 判断题目的策略类
 */
@Component
@Slf4j
public class JudgeTypeHandler implements SubjectTypeHandler{
    @Resource
    private SubjectJudgeService subjectJudgeService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        if (subjectInfoBo.getOptionList() != null){
            //判断题目的插入
            SubjectJudge subjectJudge = new SubjectJudge();
            SubjectAnswerBo subjectAnswerBO = subjectInfoBo.getOptionList().get(0);
            subjectJudge.setSubjectId(subjectInfoBo.getId());
            subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
            subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
            subjectJudgeService.insert(subjectJudge);
        }else {
            log.error("判断题插入失败，判断结果不能为空");
        }
    }

    @Override
    public SubjectOptionBo query(Long subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        List<SubjectJudge> result = subjectJudgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBo> subjectAnswerBOList = SubjectJudgeConvert.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBo subjectOptionBo = new SubjectOptionBo();
        subjectOptionBo.setOptionList(subjectAnswerBOList);
        return subjectOptionBo;
    }
}
